package components;

import gui.Window;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.bbn.openmap.omGraphics.OMPoly;

public class ArcList {

	private NodeList nodes;
	
	private int count;
	private ArrayList<Arc> arcs;
	
	private String filePath;// = "E:/projects/java/INZYNIERKA/roads-editor/src/resources/arcs.txt";
	
	private Window window;
	
	public ArcList(NodeList nodes, Window window){
		count = 0;
		arcs = new ArrayList<Arc>();
		
		this.nodes = nodes;
		this.window = window;
		
		filePath = "E:/projects/java/INZYNIERKA/roads-editor/src/resources/arcs.txt";
		
		loadArcsToList();
	}
	
	public ArcList(NodeList nodes, String filePath, Window window){
		count = 0;
		arcs = new ArrayList<Arc>();
		
		this.nodes = nodes;
		this.window = window;
		
		this.filePath = filePath;
		
		loadArcsToList();
	}

	private void loadArcsToList() {
		File file = new File(filePath);
		BufferedReader reader = null;
		
		try{
			reader = new BufferedReader(new FileReader(file));
			String line = null;
			
			if((line = reader.readLine()) != null){
				addCount(line);
			}
			int i=1;
			while((line = reader.readLine()) != null){
				arcs.add( addArc(line, i) );
				i++;
			}
		}catch(FileNotFoundException e){
			System.out.println("Nie odnaleziono pliku ["+ filePath +"]");
		}catch(IOException e){
			System.out.println("IO exception\n"+e);
		}
		finally{
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public ArrayList<Arc> getArcs() {
		return arcs;
	}

	public void setArcs(ArrayList<Arc> arcs) {
		this.arcs = arcs;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	private Arc addArc(String line, int lineNumber) {
		String[] split = line.split(" ");
		int[] arcVals = new int[6];
		ArrayList<Coordinate> cords = new ArrayList<Coordinate>();
		int tmp = 0;
		
//		for(int i=0;i<split.length;i++){
//			if(split[i].equals("")){
//				System.out.print(" ");
//			}
//			else{
//				System.out.print(split[i]);
//			}
//		}
//		System.out.println();
		
		for(int i=0,j=0; i<split.length; i++){
			if(!split[i].equals("")){
				if(j<6){
					arcVals[j] = Integer.parseInt(split[i].trim());
				}
				else if(arcVals[5] != 0){
					if(j%2 == 0){
						tmp = Integer.parseInt(split[i].trim());
//						if(cords.size()<2)
//							System.out.println(tmp);
					}
					else{
						cords.add(new Coordinate(Integer.parseInt(split[i].trim()),tmp));//TODO zmiana tmp miejscami
//						if(cords.size()<=2)
//							System.out.println("-------"+Integer.parseInt(split[i].trim()));
					}
				}
				
				j++;
			}
		}
		return new Arc(lineNumber, nodes.getNodeById(arcVals[0]), nodes.getNodeById(arcVals[1]), arcVals[2], arcVals[3], arcVals[4], arcVals[5], cords);
	}

	private void addCount(String line) {
		count = Integer.parseInt(line.trim());
	}
	
	public static void main(String args[]){
		NodeList n = new NodeList();
		ArcList a = new ArcList(n,null);
		System.out.println(a.getCount());
		System.out.print(a.getArcs().get(0).getStartNode().getLattitude()+" "+a.getArcs().get(0).getEndNode().getId());
		System.out.print(" "+a.getArcs().get(0).getBreakCount());
		for(int i=0; i<a.getArcs().get(0).getBreakCount(); i++){
			System.out.print(" "+ a.getArcs().get(0).getBreakCords().get(i).getLattitude()+" "+a.getArcs().get(0).getBreakCords().get(i).getLongtitude());
		}
		System.out.println();
		System.out.println(a.getArcs().get(1).getStartNode().getId()+" "+a.getArcs().get(1).getEndNode().getId());
		System.out.println(a.getArcs().get(3).getStartNode().getId()+" "+a.getArcs().get(3).getEndNode().getId());
		System.out.println(a.getArcs().get(4).getStartNode().getId()+" "+a.getArcs().get(4).getEndNode().getId());
	}

	public int findGraphicPosition(OMPoly object) {
		float[] cords = object.getLatLonArray();
		int pos = -1;
		
		for(int i=0; i<arcs.size(); i++){
			if(cords[0] == arcs.get(i).getStartNode().getLattitude() && cords[1] == arcs.get(i).getStartNode().getLongtitude()){
				if(cords[cords.length-2] == arcs.get(i).getEndNode().getLattitude() && cords[cords.length-1] == arcs.get(i).getEndNode().getLongtitude()){
					pos = i;
					break;
				}
			}
			else if(cords[0] == arcs.get(i).getEndNode().getLattitude() && cords[0] == arcs.get(i).getEndNode().getLongtitude()){
				if(cords[cords.length-2] == arcs.get(i).getStartNode().getLattitude() && cords[1] == arcs.get(cords.length-1).getStartNode().getLongtitude()){
					pos = i;
					break;
				}
			}
		}
		
		return pos;
	}

	public void changeLine(int pos) {
		for(int i=pos+1; i<arcs.size(); i++){
			arcs.get(i).decrementLine();
		}
	}

	public int findByLine(int line) {
		int pos = -1;
		for(int i=0; i<arcs.size(); i++){
			if(arcs.get(i).getLine() == line){
				pos = i;
				break;
			}
		}
		return pos;
	}

	public void deleteIncludeNode(int id) {
		ArrayList<Integer> ids = new ArrayList<Integer>();
		for(int i=0; i<arcs.size(); i++){
			if(arcs.get(i).getStartNode().getId() == id || arcs.get(i).getEndNode().getId() == id){
				window.getArcLayer().deleteOMGraphicAt(i);
				ids.add(i);
			}
		}
		for(int i=0; i<ids.size(); i++){
			changeLine(i);
			arcs.remove(ids.get(i));
		}
	}
	
}
